<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<LINK REL=STYLESHEET TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
<META NAME="GENERATOR" CONTENT="Java2HTML Version 1.3.1">
<TITLE>jena.examples.ontology.persistentOntology.Main (Java2HTML)</TITLE>
</HEAD>
<BODY><TABLE id="Header" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="2" width="33%">&nbsp;</td>
<td align="center" colspan="2" width="33%">
<font size="4">Main.java</font>
</td>
<td align="right" colspan="2" width="33%">&nbsp;</td>
</tr>
</TABLE>
<pre ID="Classes">
<A NAME="1"></A><FONT ID="FormalComment">/*****************************************************************************
<A NAME="2"></A> * Source code information
<A NAME="3"></A> * -----------------------
<A NAME="4"></A> * Original author    Ian Dickinson, HP Labs Bristol
<A NAME="5"></A> * Author email       ian.dickinson@hp.com
<A NAME="6"></A> * Package            Jena 2
<A NAME="7"></A> * Web                http://jena.sourceforge.net
<A NAME="8"></A> * Created            22-Aug-2003
<A NAME="9"></A> * Filename           $RCSfile: Main.java.html,v $
<A NAME="10"></A> * Revision           $Revision: 1.5 $
<A NAME="11"></A> * Release status     $State: Exp $
<A NAME="12"></A> *
<A NAME="13"></A> * Last modified on   $Date: 2007/01/17 10:44:23 $
<A NAME="14"></A> *               by   $Author: andy_seaborne $
<A NAME="15"></A> *
<A NAME="16"></A> * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
<A NAME="17"></A> * (see footer for full conditions)
<A NAME="18"></A> *****************************************************************************/</FONT>
<A NAME="19"></A>
<A NAME="20"></A><FONT ID="SingleLineComment">// Package
<A NAME="21"></A></FONT><FONT ID="SingleLineComment">///////////////
<A NAME="22"></A></FONT><FONT ID="Package">package</FONT> jena.examples.ontology.persistentOntology;
<A NAME="23"></A>
<A NAME="24"></A>
<A NAME="25"></A><FONT ID="SingleLineComment">// Imports
<A NAME="26"></A></FONT><FONT ID="SingleLineComment">///////////////
<A NAME="27"></A></FONT><FONT ID="Import">import</FONT> com.hp.hpl.jena.ontology.*;
<A NAME="28"></A><FONT ID="Import">import</FONT> com.hp.hpl.jena.rdf.model.ModelMaker;
<A NAME="29"></A>
<A NAME="30"></A>
<A NAME="31"></A><FONT ID="FormalComment">/**
<A NAME="32"></A> * &lt;p&gt;
<A NAME="33"></A> * Simple execution wrapper for persistent ontology example.
<A NAME="34"></A> * &lt;/p&gt;
<A NAME="35"></A> * &lt;p&gt;
<A NAME="36"></A> * Usage:
<A NAME="37"></A> * &lt;pre&gt;
<A NAME="38"></A> * java jena.examples.ontology.persistentOntology.Main
<A NAME="39"></A> *                  [--dbUser string]     e.g: --dbUser ijd
<A NAME="40"></A> *                  [--dbURL string]      e.g: --dbURL jdbc:postgresql://localhost/jenatest
<A NAME="41"></A> *                  [--dbPw string]       e.g: --dbPw nosecrets
<A NAME="42"></A> *                  [--dbType string]     e.g: --dbType PostgreSQL
<A NAME="43"></A> *                  [--dbDriver string]   e.g: --dbDriver org.postgresql.Driver
<A NAME="44"></A> *                  [--reload]            if true will reload the source data
<A NAME="45"></A> *                  [sourceURL]           optional source URL for the data to persist
<A NAME="46"></A> * &lt;/pre&gt;
<A NAME="47"></A> * If no db parameters or source URL is given, defaults will be used.
<A NAME="48"></A> * &lt;/p&gt;
<A NAME="49"></A> *
<A NAME="50"></A> * @author Ian Dickinson, HP Labs
<A NAME="51"></A> *         (&lt;a  href="mailto:Ian.Dickinson@hp.com" &gt;email&lt;/a&gt;)
<A NAME="52"></A> * @version CVS $Id: Main.java.html,v 1.5 2007/01/17 10:44:23 andy_seaborne Exp $
<A NAME="53"></A> */</FONT>
<A NAME="54"></A><FONT ID="Public">public</FONT> <FONT ID="Class">class</FONT> Main {
<A NAME="55"></A>    <FONT ID="SingleLineComment">// Constants
<A NAME="56"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="57"></A></FONT>
<A NAME="58"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> <FONT ID="Final">final</FONT> String ONT1 = <FONT ID="StringLiteral">"urn:x-hp-jena:test1"</FONT>;
<A NAME="59"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> <FONT ID="Final">final</FONT> String ONT2 = <FONT ID="StringLiteral">"urn:x-hp-jena:test2"</FONT>;
<A NAME="60"></A>
<A NAME="61"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> <FONT ID="Final">final</FONT> String DB_URL = <FONT ID="StringLiteral">"jdbc:postgresql://localhost/jenatest"</FONT>;
<A NAME="62"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> <FONT ID="Final">final</FONT> String DB_USER = <FONT ID="StringLiteral">"ijd"</FONT>;
<A NAME="63"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> <FONT ID="Final">final</FONT> String DB_PASSWD = <FONT ID="StringLiteral">""</FONT>;
<A NAME="64"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> <FONT ID="Final">final</FONT> String DB = <FONT ID="StringLiteral">"PostgreSQL"</FONT>;
<A NAME="65"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> <FONT ID="Final">final</FONT> String DB_DRIVER = <FONT ID="StringLiteral">"org.postgresql.Driver"</FONT>;
<A NAME="66"></A>
<A NAME="67"></A>    <FONT ID="SingleLineComment">// Static variables
<A NAME="68"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="69"></A></FONT>
<A NAME="70"></A>    <FONT ID="SingleLineComment">// database connection parameters, with defaults
<A NAME="71"></A></FONT>    <FONT ID="Private">private</FONT> <FONT ID="Static">static</FONT> String s_dbURL = DB_URL;
<A NAME="72"></A>    <FONT ID="Private">private</FONT> <FONT ID="Static">static</FONT> String s_dbUser = DB_USER;
<A NAME="73"></A>    <FONT ID="Private">private</FONT> <FONT ID="Static">static</FONT> String s_dbPw = DB_PASSWD;
<A NAME="74"></A>    <FONT ID="Private">private</FONT> <FONT ID="Static">static</FONT> String s_dbType = DB;
<A NAME="75"></A>    <FONT ID="Private">private</FONT> <FONT ID="Static">static</FONT> String s_dbDriver = DB_DRIVER;
<A NAME="76"></A>
<A NAME="77"></A>    <FONT ID="SingleLineComment">// if true, reload the data
<A NAME="78"></A></FONT>    <FONT ID="Private">private</FONT> <FONT ID="Static">static</FONT> <FONT ID="Boolean">boolean</FONT> s_reload = <FONT ID="False">false</FONT>;
<A NAME="79"></A>
<A NAME="80"></A>    <FONT ID="SingleLineComment">// source URL to load data from; if null, use default
<A NAME="81"></A></FONT>    <FONT ID="Private">private</FONT> <FONT ID="Static">static</FONT> String s_source;
<A NAME="82"></A>
<A NAME="83"></A>
<A NAME="84"></A>    <FONT ID="SingleLineComment">// Instance variables
<A NAME="85"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="86"></A></FONT>
<A NAME="87"></A>
<A NAME="88"></A>    <FONT ID="SingleLineComment">// Constructors
<A NAME="89"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="90"></A></FONT>
<A NAME="91"></A>    <FONT ID="SingleLineComment">// External signature methods
<A NAME="92"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="93"></A></FONT>
<A NAME="94"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> <FONT ID="Void">void</FONT> main( String[] args ) {
<A NAME="95"></A>        processArgs( args );
<A NAME="96"></A>
<A NAME="97"></A>        <FONT ID="SingleLineComment">// check for default sources
<A NAME="98"></A></FONT>        <FONT ID="If">if</FONT> (s_source == <FONT ID="Null">null</FONT>) {
<A NAME="99"></A>            s_source = getDefaultSource();
<A NAME="100"></A>        }
<A NAME="101"></A>
<A NAME="102"></A>        <FONT ID="SingleLineComment">// create the helper class we use to handle the persistent ontologies
<A NAME="103"></A></FONT>        <A HREF="PersistentOntology.java.html">PersistentOntology</A> po = <FONT ID="New">new</FONT> <A HREF="PersistentOntology.java.html">PersistentOntology</A>();
<A NAME="104"></A>
<A NAME="105"></A>        <FONT ID="SingleLineComment">// ensure the JDBC driver class is loaded
<A NAME="106"></A></FONT>        <FONT ID="Try">try</FONT> {
<A NAME="107"></A>            Class.forName( s_dbDriver );
<A NAME="108"></A>        }
<A NAME="109"></A>        <FONT ID="Catch">catch</FONT> (Exception e) {
<A NAME="110"></A>            System.err.println( <FONT ID="StringLiteral">"Failed to load the driver for the database: "</FONT> + e.getMessage() );
<A NAME="111"></A>            System.err.println( <FONT ID="StringLiteral">"Have you got the CLASSPATH set correctly?"</FONT> );
<A NAME="112"></A>        }
<A NAME="113"></A>
<A NAME="114"></A>        <FONT ID="SingleLineComment">// are we re-loading the data this time?
<A NAME="115"></A></FONT>        <FONT ID="If">if</FONT> (s_reload) {
<A NAME="116"></A>
<A NAME="117"></A>            <FONT ID="SingleLineComment">// we pass cleanDB=true to clear out existing models
<A NAME="118"></A></FONT>            <FONT ID="SingleLineComment">// NOTE: this will remove ALL Jena models from the named persistent store, so
<A NAME="119"></A></FONT>            <FONT ID="SingleLineComment">// use with care if you have existing data stored
<A NAME="120"></A></FONT>            ModelMaker maker = po.getRDBMaker( s_dbURL, s_dbUser, s_dbPw, s_dbType, <FONT ID="True">true</FONT> );
<A NAME="121"></A>
<A NAME="122"></A>            <FONT ID="SingleLineComment">// now load the source data into the newly cleaned db
<A NAME="123"></A></FONT>            po.loadDB( maker, s_source );
<A NAME="124"></A>        }
<A NAME="125"></A>
<A NAME="126"></A>        <FONT ID="SingleLineComment">// now we list the classes in the database, to show that the persistence worked
<A NAME="127"></A></FONT>        ModelMaker maker = po.getRDBMaker( s_dbURL, s_dbUser, s_dbPw, s_dbType, <FONT ID="False">false</FONT> );
<A NAME="128"></A>        po.listClasses( maker, s_source );
<A NAME="129"></A>    }
<A NAME="130"></A>
<A NAME="131"></A>
<A NAME="132"></A>    <FONT ID="SingleLineComment">// Internal implementation methods
<A NAME="133"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="134"></A></FONT>
<A NAME="135"></A>    <FONT ID="FormalComment">/**
<A NAME="136"></A>     * Process any command line arguments
<A NAME="137"></A>     */</FONT>
<A NAME="138"></A>    <FONT ID="Private">private</FONT> <FONT ID="Static">static</FONT> <FONT ID="Void">void</FONT> processArgs( String[] args ) {
<A NAME="139"></A>        <FONT ID="Int">int</FONT> i = <FONT ID="IntegerLiteral">0</FONT>;
<A NAME="140"></A>        <FONT ID="While">while</FONT> (i &lt; args.length) {
<A NAME="141"></A>            String arg = args[i++];
<A NAME="142"></A>
<A NAME="143"></A>            <FONT ID="If">if</FONT>      (arg.equals( <FONT ID="StringLiteral">"--dbUser"</FONT> ))   {s_dbURL = args[i++];}
<A NAME="144"></A>            <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (arg.equals( <FONT ID="StringLiteral">"--dbURL"</FONT> ))    {s_dbURL = args[i++];}
<A NAME="145"></A>            <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (arg.equals( <FONT ID="StringLiteral">"--dbPasswd"</FONT> )) {s_dbPw = args[i++];}
<A NAME="146"></A>            <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (arg.equals( <FONT ID="StringLiteral">"--dbType"</FONT> ))   {s_dbType = args[i++];}
<A NAME="147"></A>            <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (arg.equals( <FONT ID="StringLiteral">"--reload"</FONT> ))   {s_reload = <FONT ID="True">true</FONT>;}
<A NAME="148"></A>            <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (arg.equals( <FONT ID="StringLiteral">"--dbDriver"</FONT> )) {s_dbDriver = args[i++];}
<A NAME="149"></A>            <FONT ID="Else">else</FONT> {
<A NAME="150"></A>                <FONT ID="SingleLineComment">// assume this is a URL to load data from
<A NAME="151"></A></FONT>                s_source = arg;
<A NAME="152"></A>            }
<A NAME="153"></A>        }
<A NAME="154"></A>    }
<A NAME="155"></A>
<A NAME="156"></A>    <FONT ID="FormalComment">/**
<A NAME="157"></A>     * Answer the default source document, and set up the document manager
<A NAME="158"></A>     * so that we can find it on the file system
<A NAME="159"></A>     *
<A NAME="160"></A>     * @return The URI of the default source document
<A NAME="161"></A>     */</FONT>
<A NAME="162"></A>    <FONT ID="Private">private</FONT> <FONT ID="Static">static</FONT> String getDefaultSource() {
<A NAME="163"></A>        <FONT ID="SingleLineComment">// use the ont doc mgr to map from a generic URN to a local source file
<A NAME="164"></A></FONT>        OntDocumentManager.getInstance().addAltEntry( ONT1, <FONT ID="StringLiteral">"file:src-examples/data/test1.owl"</FONT> );
<A NAME="165"></A>        OntDocumentManager.getInstance().addAltEntry( ONT2, <FONT ID="StringLiteral">"file:src-examples/data/test2.owl"</FONT> );
<A NAME="166"></A>
<A NAME="167"></A>        <FONT ID="Return">return</FONT> ONT1;
<A NAME="168"></A>    }
<A NAME="169"></A>
<A NAME="170"></A>
<A NAME="171"></A>    <FONT ID="SingleLineComment">//==============================================================================
<A NAME="172"></A></FONT>    <FONT ID="SingleLineComment">// Inner class definitions
<A NAME="173"></A></FONT>    <FONT ID="SingleLineComment">//==============================================================================
<A NAME="174"></A></FONT>
<A NAME="175"></A>}
<A NAME="176"></A>
<A NAME="177"></A>
<A NAME="178"></A><FONT ID="MultiLineComment">/*
<A NAME="179"></A>    (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
<A NAME="180"></A>    All rights reserved.
<A NAME="181"></A>
<A NAME="182"></A>    Redistribution and use in source and binary forms, with or without
<A NAME="183"></A>    modification, are permitted provided that the following conditions
<A NAME="184"></A>    are met:
<A NAME="185"></A>
<A NAME="186"></A>    1. Redistributions of source code must retain the above copyright
<A NAME="187"></A>       notice, this list of conditions and the following disclaimer.
<A NAME="188"></A>
<A NAME="189"></A>    2. Redistributions in binary form must reproduce the above copyright
<A NAME="190"></A>       notice, this list of conditions and the following disclaimer in the
<A NAME="191"></A>       documentation and/or other materials provided with the distribution.
<A NAME="192"></A>
<A NAME="193"></A>    3. The name of the author may not be used to endorse or promote products
<A NAME="194"></A>       derived from this software without specific prior written permission.
<A NAME="195"></A>
<A NAME="196"></A>    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
<A NAME="197"></A>    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
<A NAME="198"></A>    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
<A NAME="199"></A>    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
<A NAME="200"></A>    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
<A NAME="201"></A>    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
<A NAME="202"></A>    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
<A NAME="203"></A>    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<A NAME="204"></A>    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
<A NAME="205"></A>    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<A NAME="206"></A>*/</FONT>
<A NAME="207"></A>
<A NAME="208"></A></pre>
</BODY>
</HTML>